package com.dyoud.api.user.service;

import java.util.List;

import javax.annotation.Resource;

import org.douyoudian.utils.CommonUtils;
import org.douyoudian.utils.ReplaceStarUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dyoud.api.ReturnInfo;
import com.dyoud.api.exception.ApiExcepiton;
import com.dyoud.api.user.dto.BankAddDto;
import com.dyoud.api.user.dto.BankInfoDto;
import com.dyoud.api.user.dto.CheckBankInfoDto;
import com.dyoud.api.user.dto.UnbindBankInfoDto;
import com.dyoud.core.DozerUtils;
import com.dyoud.core.code.SmsManager;
import com.dyoud.core.enums.SmsTypeEnum;
import com.dyoud.user.api.enums.BankStatusEnum;
import com.dyoud.user.api.enums.BankTypeEnum;
import com.dyoud.user.api.exception.BankException;
import com.dyoud.user.api.exception.SmsInvalidException;
import com.dyoud.user.api.model.BankInfo;
import com.dyoud.user.api.requests.bank.AddBankReqeust;
import com.dyoud.user.api.requests.bank.BankCheckRequest;
import com.dyoud.user.api.requests.bank.UnbindBankInfoRequest;
import com.dyoud.user.api.response.JuheIdentifireInfoResponse;
import com.dyoud.user.api.service.BankInfoService;
import com.google.common.collect.Lists;

/**
 * 银行卡
 * 
 * @author Jonathan
 * @date 2018/03/01
 *
 */
@Service
public class BankApiService {

    @Reference
    private BankInfoService bankInfoService;

    @Resource
    private SmsManager smsManager;

    @Value("${is_dev}")
    private boolean isDev;

    /**
     * 查询用户所有银行卡
     * 
     * @param userId
     * @return
     */
    public List<BankInfoDto> listBanks(Long userId) {
        List<BankInfoDto> bankInfoDtos = Lists.newArrayList();
        List<BankInfo> banks = bankInfoService.selectByUserIdWithIsDel(userId, BankStatusEnum.NORMAL);
        for (BankInfo bankInfo : banks) {
            BankInfoDto dto = new BankInfoDto();
            DozerUtils.copy(bankInfo, dto);
            String cardNo = dto.getNo();
            // 星号替换
            cardNo = CommonUtils.replaceStar(cardNo, 4);
            // 目前只支持储蓄卡
            dto.setNo(cardNo);
            dto.setCardholder(ReplaceStarUtils.replaceNameStar(dto.getCardholder()));
            dto.setTypeName("储蓄卡");
            bankInfoDtos.add(dto);

        }
        return bankInfoDtos;
    };

    /**
     * 添加银行卡
     * 
     * @param addDto
     * @return
     */
    public boolean addBank(BankAddDto addDto) {
        boolean isOk = false;
        // 校验短信验证码
        if (!isDev) {
            isOk = smsManager.checkCode(addDto.getPhone(), addDto.getCode(), SmsTypeEnum.ADD_BANK_INFO);
            if (!isOk) {
                throw new SmsInvalidException(ReturnInfo.SMS_INVALID_MSG);
            }
        }

        AddBankReqeust bankReqeust = new AddBankReqeust();
        DozerUtils.copy(addDto, bankReqeust);
        bankReqeust.setBankTypeEnum(BankTypeEnum.SAVING_CARD);

        try {
            isOk = bankInfoService.addBank(bankReqeust) > 0;
        } catch (BankException e) {
            throw new ApiExcepiton(e.getMessage());
        }
        return isOk;

    }

    /**
     * 获取银行卡信息
     * 
     * @param userId
     * @param bankId
     * @return
     */
    public BankInfoDto getByUserId(Long userId, Long bankId) {
        BankInfoDto bankInfoDto = new BankInfoDto();
        BankInfo bankInfo = bankInfoService.getByUserId(userId, bankId);
        DozerUtils.copy(bankInfo, bankInfoDto);
        return bankInfoDto;
    }

    /**
     * 校验银行卡是否正确
     * 
     * @param userId
     * @param bankId
     * @return
     */
    public JuheIdentifireInfoResponse check(CheckBankInfoDto checkBankInfoDto) {
        if (!isDev) {
            boolean isOk = smsManager.checkCode(checkBankInfoDto.getPhone(), checkBankInfoDto.getCode(), SmsTypeEnum.CHECK_BANK);
            if (!isOk) {
                throw new SmsInvalidException("短信验证码不正确");
            }
        }
        BankCheckRequest bankCheckRequest = new BankCheckRequest();
        DozerUtils.copy(checkBankInfoDto, bankCheckRequest);
        return bankInfoService.checkBankInfo(bankCheckRequest);
    }

    /**
     * 解绑银行卡
     * 
     * @param unbindBankInfoDto
     * @return
     */
    public boolean unBindBankInfo(UnbindBankInfoDto unbindBankInfoDto) {
        UnbindBankInfoRequest request = new UnbindBankInfoRequest();
        DozerUtils.copy(unbindBankInfoDto, request);
        return bankInfoService.unBind(request);
    }

}
